package com.qdxwx.manage.controller.upload;

import com.qdxwx.common.config.UploadConfig;
import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.upload.UploadState;
import com.qdxwx.common.upload.UploadUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import jakarta.servlet.http.HttpServletRequest;
import java.util.Map;


/**
 * 图片上传通用接口Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/upload")
public class UploadController {
    /**
     * 图片上传文件
     */
    @PostMapping("/image.json")
    @ResponseBody
    public Map<String, Object> saveImage(MultipartFile file) {
        if (file.isEmpty()) {
            return AjaxResult.error("上传失败，请选择正确的文件！");
        }
        UploadState state = UploadUtils.saveImageFile(file, UploadConfig.uploadImageFolder);
        return state.getResult();
    }

    //图片上传至临时文件夹
    @PostMapping("/temp/image.json")
    @ResponseBody
    public Map<String, Object> saveTempImage(MultipartFile file) {
        if (file.isEmpty()) {
            return AjaxResult.error("上传失败，请选择正确的文件！");
        }
        UploadState state = UploadUtils.saveImageFile(file, UploadConfig.uploadTempFolder);
        return state.getResult();
    }

    /**
     * 文件上传
     */
    @PostMapping("/file.json")
    @ResponseBody
    public Map<String, Object> saveFile(HttpServletRequest request) {
        UploadState state = UploadUtils.saveFile(request);
        return state.getResult();
    }

    /**
     * 文件上传
     */
    @PostMapping("/temp/file.json")
    @ResponseBody
    public Map<String, Object> saveTempFile(HttpServletRequest request) {
        UploadState state = UploadUtils.saveFile(request, UploadConfig.uploadTempFolder);
        return state.getResult();
    }
}
